package gui;

import controlleur.ControllerGrille;
import gui.dialog.DialogNewGame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import moteur.Plateau;

public class Window extends JFrame implements WindowListener
{

    DialogNewGame newGame;
    //instancie la fenêtre et appelle d'initComponents

    public Window()
    {
        super("JOthello");
        
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        
        newGame = new DialogNewGame(this);
        initComponent();
        setSize(400, 400);

        addWindowListener(this);
        setVisible(true);
        
    }

    //crée et ajoute tous les composants de la fenêtre principal
    public void initComponent()
    {
        
        Container c = getContentPane();

        JMenuBar barMenu = new JMenuBar();

        initMenu(barMenu);

        c.add(barMenu, BorderLayout.NORTH);

        JPanel jp = new JPanel(new BorderLayout());
        JPanel jp2 = new JPanel(new FlowResizeLayout());

        jp.setBorder(BorderFactory.createLineBorder(Color.black));
        jp2.setBorder(BorderFactory.createLineBorder(Color.black));

        Grille g = Grille.getInstance();
        g.setBorder(BorderFactory.createLineBorder(Color.black));

        InfoPlat infoPlateau = new InfoPlat();
        jp2.add(g);

        jp.add(jp2, BorderLayout.CENTER);
        jp.add(infoPlateau, BorderLayout.SOUTH);


        c.add(jp, BorderLayout.CENTER);

        setContentPane(c);
    }

    /*Ferme l'application
     * Tout en faisant des traitements avant la fermeture
     */
    public void closeWindow() {
        if (Plateau.getInstance().getJ1().toChar() != 'N' || Plateau.getInstance().getJ2().toChar() != 'N') {
            int rep = JOptionPane.showConfirmDialog(this, "Voulez vous sauvegarder la partie en cours ?", "Sauvegarde", JOptionPane.YES_NO_OPTION);
            if (rep == JOptionPane.YES_OPTION) {
                Plateau.getInstance().save();
            }
        }
        this.setVisible(false);
        this.dispose();
    }

    public void newGame()
    {
        newGame.setVisible(true);
        newGame.setLocationRelativeTo(this);
    }

    public void windowOpened(WindowEvent e)
    {
        if(Plateau.getInstance().isSaveFile()){
            int rep = JOptionPane.showConfirmDialog(this, "Voulez vous charger la partie sauvegarder ?", "Chargement", JOptionPane.YES_NO_OPTION);
            if (rep == JOptionPane.YES_OPTION) {
                Plateau.getInstance().load();
            }
        }
    }

    public void windowClosing(WindowEvent e)
    {
        closeWindow();
    }

    public void windowClosed(WindowEvent e)
    {
        closeWindow();
    }

    public void windowIconified(WindowEvent e)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeiconified(WindowEvent e)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowActivated(WindowEvent e)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeactivated(WindowEvent e)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @param   barMenu <i>JMenuBar</i> passer en parametre un JMenuBar pour ajouter tout
     */
    private void initMenu(JMenuBar barMenu)
    {
        JMenu menuPartie = new JMenu("Partie");

        JMenuItem novPartie = new JMenuItem("Nouvelle");
        novPartie.addActionListener(new ActionListener()
        {

            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                newGame();
            }
        });
        novPartie.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_MASK));

        JMenuItem quitGame = new JMenuItem("Quitter");
        quitGame.addActionListener(new ActionListener()
        {

            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                closeWindow();
            }
        });
        quitGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK));

        menuPartie.add(novPartie);
        menuPartie.add(quitGame);
        
        barMenu.add(menuPartie);
    }
    
    public void peauNeuve(){
        new ControllerGrille().newPartie();
        Container c = getContentPane();
        c.removeAll();
        setContentPane(c);
        this.initComponent();
        this.repaint();
    }
}
